<?php
class User extends CI_Model {
	
	var $TableName = "Users";

    function __construct()
    {
        parent::__construct();
		
		//load the default database
		$this->load->database();
		
    }
	
	function getUserById($UserId)
	{
		$query = $this->db->get_where($this->TableName, array(
				"UserId" => $UserId
		));
							
		if($query->num_rows() > 0)
		{
			return $query->row();
		}
		return FALSE;
	}
	
	function getUserByCellPhone($CellPhone)
	{
		$query = $this->db->get_where($this->TableName, array(
				"CellPhone" => $CellPhone
		));
							
		if($query->num_rows() > 0)
		{
			return $query->row();
		}
		return FALSE;
	}
	
	function getUserByEMail($EMail)
	{
		$query = $this->db->get_where($this->TableName, array(
				"EMail" => $EMail
		));
							
		if($query->num_rows() > 0)
		{
			return $query->row();
		}
		return FALSE;
	}
		
	function checkPassword($UserId, $Password, $hashed = false)
	{
						
		if($hashed === FALSE)
		{
			$Password = md5($Password);
		}
				
		$query = NULL;
		$id_field = NULL;
		
		if(preg_match("/^[0-9X]{18,18}$/", $UserId))
		{
			$id_field = "UserId";
		}
		
		if(preg_match("/^[0-9]{11,11}$/", $UserId))
		{
			$id_field = "CellPhone";
		}
		
		if(preg_match("/^[^@]*@[^@]*\.[^@]*$/", $UserId))
		{
			$id_field = "Email";
		}
		
		
		if($id_field != NULL)
		{
			$query = $this->db->get_where($this->TableName, array(
					$id_field => $UserId,
					"Password" => $Password
			));
			
			//echo $this->db->last_query();
						
			if($query->num_rows() > 0)
			{
				return $query->row();
			}
		}
				
		return FALSE;
		
	}
	
	
	function createUser($UserId, $UserName, $Password, $CellPhone, $EMail)
	{
		$data = array(
		   'UserId' => $UserId ,
		   'UserName' => $UserName ,
		   'Password' => md5($Password) ,
		   'CellPhone' => $CellPhone ,
		   'EMail' => $EMail ,
		   'Sex' => 1
		);
		
		$this->db->insert($this->TableName, $data); 
		
		return $this->getUserById($UserId);
	}
	
	function updateUser($UserId, $UserName, $CellPhone, $EMail)
	{
		$data = array(
			'UserName' => $UserName ,
			'CellPhone' => $CellPhone ,
			'EMail' => $EMail
		);

		$this->db->where('UserId', $UserId);
		$this->db->update($this->TableName, $data); 
		
		$query = $this->db->get_where($this->TableName, array(
					"UserId" => $UserId
		));
		return $query->row();
	}
	
	function updatePasswordDirect($user, $NewPassword)
	{
		
		if(! $this->__PasswordVerify($NewPassword))
		{
			return FALSE;
		}
		
		$data = array(
            'Password' => md5($NewPassword)
        );

		$this->db->where('UserId', $user->UserId);
		$this->db->update($this->TableName, $data); 
		
		$query = $this->db->get_where($this->TableName, array(
					"UserId" => $user->UserId
		));
		return $query->row();
	}
	
	function updatePassword($user, $OldPassword, $NewPassword)
	{
		
		if(! $this->__PasswordVerify($NewPassword))
		{
			return FALSE;
		}
				
		$query = $this->db->get_where($this->TableName, array(
					"UserId" => $user->UserId,
					"Password" => md5($OldPassword)
		));
		
		if($query->num_rows() == 0)
		{
			return FALSE;
		}
		
		$data = array(
            'Password' => md5($NewPassword)
        );

		$this->db->where('UserId', $user->UserId);
		$this->db->update($this->TableName, $data); 
		
		$query = $this->db->get_where($this->TableName, array(
					"UserId" => $user->UserId
		));
		return $query->row();
	}
	
	function __PasswordVerify($Password)
	{
		//TODO
		return true;
	}
	
	function verifyUser($UserId, $UserName, $Password, $CellPhone, $EMail)
	{
		
		$chinese_pattern = "\x80-\xff";
		
		if(!preg_match("/^[0-9X]{18,18}$/", $UserId))
			return FALSE;
		
		if(!preg_match("/^[0-9]{11,11}$/", $CellPhone))
			return FALSE;
		
		if(!preg_match("/^[^@]*@[^@]*\.[^@]*$/", $EMail))
			return FALSE;
		
		if(!preg_match("/^[$chinese_pattern]*$/", $UserName))
			return FALSE;
		
		if(!$this->__PasswordVerify($Password))
			return FALSE;
		
		if($this->getUserById($UserId) !== FALSE)
			return FALSE;
		
		if($this->getUserByEMail($EMail) !== FALSE)
			return FALSE;
		
		if($this->getUserByCellPhone($CellPhone) !== FALSE)
			return FALSE;
		
		return TRUE;
	}
	
	function verifyUserUpdate($user, $UserName, $CellPhone, $EMail)
	{
		
		$chinese_pattern = "\x80-\xff";
		
		if(!preg_match("/^[0-9]{11,11}$/", $CellPhone))
			return FALSE;
		
		if(!preg_match("/^[^@]*@[^@]*\.[^@]*$/", $EMail))
			return FALSE;
		
		if(!preg_match("/^[$chinese_pattern]*$/", $UserName))
			return FALSE;
		
		
		/*if($EMail != $user->EMail && $this->getUserByEMail($EMail) !== FALSE)
			return FALSE;
		
		if($CellPhone != $user->CellPhone && $this->getUserByCellPhone($CellPhone) !== FALSE)
			return FALSE;*/
		
		return TRUE;
	}
}
?>